#include "distance_config.h"

using namespace std;
using namespace cv;

namespace DistanceConfig {

CalibrationConfig::CalibrationConfig() : boardSize(0, 0), boardSquareSize(0.0)
{
}

bool CalibrationConfig::readConfigFrom(const string& filename)
{
	FileStorage fs;
	if (!fs.open(filename, FileStorage::READ)) {
		return false;
	}

	fs["board_width"] >> this->boardSize.width;
	fs["board_height"] >> this->boardSize.height;
	fs["square_size"] >> this->boardSquareSize;

	fs.release();
	return true;
}

bool CalibrationConfig::readResultFrom(const string& filename)
{
	FileStorage fs;
	if (!fs.open(filename, FileStorage::READ)) {
		return false;
	}

	fs["camera_matrix"] >> this->cameraMatrix;
	fs["dist_coeffs"] >> this->distCoeffs;

	fs.release();
	return true;
}

bool CalibrationConfig::writeConfigTo(const string& filename)
{
	FileStorage fs;
	if (!fs.open(filename, FileStorage::WRITE)) {
		return false;
	}

	fs << "board_width" << this->boardSize.width;
	fs << "board_height" << this->boardSize.height;
	fs << "square_size" << this->boardSquareSize;

	fs.release();
	return true;
}

bool CalibrationConfig::writeResultTo(const string& filename)
{
	FileStorage fs;
	if (!fs.open(filename, FileStorage::WRITE)) {
		return false;
	}

	fs << "camera_matrix" << this->cameraMatrix;
	fs << "dist_coeffs" << this->distCoeffs;

	fs.release();
	return true;

}


DistanceConfig::DistanceConfig() :
	focalLength(0),
	cellSize(0, 0),
	radius(0),
	squareLength(0),
	reference(0),
	scale(0)
{
}

bool DistanceConfig::readFrom(const std::string& filename)
{
	FileStorage fs;
	if (!fs.open(filename, FileStorage::READ)) {
		return false;
	}

	fs["focal_length"] >> this->focalLength;
	fs["x_cell_size"] >> this->cellSize.width;
	fs["y_cell_size"] >> this->cellSize.height;
	fs["object_radius"] >> this->radius;
	fs["square_length"] >> this->squareLength;
	fs["reference"] >> this->reference;
	fs["scale"] >> this->scale;

	fs.release();
	return true;
}

bool DistanceConfig::writeTo(const std::string& filename)
{
	FileStorage fs;
	if (!fs.open(filename, FileStorage::WRITE)) {
		return false;
	}

	fs << "focal_length" << this->focalLength;
	fs << "x_cell_size" << this->cellSize.width;
	fs << "y_cell_size" << this->cellSize.height;
	fs << "object_radius" << this->radius;
	fs << "square_length" << this->squareLength;
	fs << "reference" << this->reference;
	fs << "scale" << this->scale;

	fs.release();
	return true;
}

};